package spr

import (
	pl "db2s/Or/spr/parser"
	"fmt"
	"github.com/antlr4-go/antlr/v4"
	"reflect"
)

func selectSingleElementExpression(node antlr.RuleNode, c *ColumnCondition) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
			//fmt.Println("-----ppp:", reflect.TypeOf(node), childNode.GetText())
		case *pl.Function_callContext:
			var f = new(FunctionCallS)
			functionCall(childNode, f)
			c.Func = *f
		case *pl.AliasContext:
			c.AS = Alias(childNode)
		case *pl.VariableContext:
			c.Value = childNode.GetText()
		default:
			//fmt.Println("default func=selectSingleElementExpression():", reflect.TypeOf(childNode), childNode.(antlr.RuleNode).GetText())
			selectSingleElementExpression(childNode.(antlr.RuleNode), c)
		}
	}
	return
}

// select 查询列
func selectList(node antlr.RuleNode) (res []ColumnCondition) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
		case *pl.Select_list_element_expression_specContext:
			var c = new(ColumnCondition)
			selectSingleElementExpression(childNode, c)
			res = append(res, *c)
		default:
			//fmt.Println("default func=selectList():", reflect.TypeOf(childNode), childNode.(antlr.RuleNode).GetText())
			//selectList(childNode.(antlr.RuleNode))
		}
	}
	return
}
func fromTableReferenceContext(node antlr.RuleNode, c *FromCondition) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
		case *pl.Query_table_expression_object_specContext:
		case *pl.Query_table_expression_table_collection_specContext:
		case *pl.Query_table_expression_subquery_specContext:
			c.Type = "subquery"
		case *pl.Query_table_expression_simple_specContext:
			c.Type = "table"
			c.Value = childNode.GetText()
			fmt.Println("-----kkk:", childNode.GetText())
		default:
			fmt.Println("--------default func=fromTableReferenceContext():", reflect.TypeOf(childNode), childNode.(antlr.RuleNode).GetText())
			//fromTableReferenceContext(childNode.(antlr.RuleNode), c)
		}
	}
}
func fromClauseDeclaration(node antlr.RuleNode, c *FromCondition) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
		case *pl.Table_referenceContext:
			c.Type = "table"
			fromTableReferenceContext(childNode, c)
		case *pl.Inline_analytic_viewContext:
			c.Type = "view"
		default:
			//fmt.Println("--------default func=fromClauseDeclaration():", reflect.TypeOf(childNode), childNode)
			//fromClauseDeclaration(childNode.(antlr.RuleNode), c)
		}
	}
}
func fromList(node antlr.RuleNode) (res []FromCondition) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *antlr.TerminalNodeImpl:
		case *pl.From_clause_declarationContext:
			var fromCondition = new(FromCondition)
			fromClauseDeclaration(childNode, fromCondition)
			res = append(res, *fromCondition)
		default:
			//fmt.Println("--------default func=fromList():", reflect.TypeOf(childNode), childNode)
		}
	}
	return
}

func queryBlock(node antlr.RuleNode) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *pl.With_clauseContext:
		case *pl.Selected_listContext:
			for _, v := range selectList(childNode) {
				fmt.Println("------kkk:", v)
			}
		case *pl.From_clauseContext:
			for _, v := range fromList(childNode) {
				fmt.Println("------fff:", v)
			}
		case *pl.Where_clauseContext:
			for _, v := range whereClauseContext(childNode) {
				fmt.Println("------w:", v)
			}
		case *pl.Group_by_clauseContext:
		case *antlr.TerminalNodeImpl:
		default:
			fmt.Println("--------default:", reflect.TypeOf(childNode), childNode)
		}
	}
}
func subQueryElement(node antlr.RuleNode) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *pl.Query_blockContext:
			queryBlock(childNode)
		default:
			//fmt.Println("-----default:", reflect.TypeOf(childNode), childNode.(antlr.RuleNode).GetText())
		}
	}
}

// select union all select
func subQueryOperation(node antlr.RuleNode) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *pl.Subquery_operationContext:
		case *pl.Subquery_elementContext:
			subQueryElement(childNode)
		}
	}
}
func orderByClause(node antlr.RuleNode) {

}
func LimitClause(node antlr.RuleNode) {

}
func subQuery(node antlr.RuleNode) {
	var headBit = make([]int, 4)
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *pl.Subquery_elementContext: //subquery_element
			headBit[0] = 1
			subQueryElement(childNode)
		case *pl.Subquery_operation_partContext: //subquery_operation_part
			subQueryOperation(childNode)
			headBit[1] = 1
		case *pl.Order_by_clauseContext: //order_by_clause
			orderByClause(childNode)
			headBit[2] = 1
		case *pl.Row_limiting_clauseContext: //row_limiting_clause
			LimitClause(childNode)
			headBit[3] = 1
		}
	}
}
func selectParse(node antlr.RuleNode) {
	for _, child := range node.GetChildren() {
		switch childNode := child.(type) {
		case *pl.SubqueryContext:
			subQuery(childNode)
			return
		case antlr.TerminalNode:
		default:
			//fmt.Println("-------default:", reflect.TypeOf(childNode), childNode.GetChildren())
			//selectParse(childNode.(antlr.RuleNode))
		}
	}
}
